package com.wwwarehouse.financialcenter.fragment.moneywarehouse.homepage;


import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.ElasticScrollView;
import com.wwwarehouse.financialcenter.R;
import com.wwwarehouse.financialcenter.bean.bindingcard.BindCardBean;
import com.wwwarehouse.financialcenter.bean.bindingcard.PermissionBean;
import com.wwwarehouse.financialcenter.constant.FinancialConstant;
import com.wwwarehouse.financialcenter.fragment.base.TheParentFragment;
import com.wwwarehouse.financialcenter.fragment.moneywarehouse.bankcard.UnbindCardInfoFragment;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description:
 * Copyright  : Copyright (c) 2017
 * Author     : yfwang
 * Date       : 2017/11/14 13:42
 */
public class CardInfoFragment extends TheParentFragment {

    private TextView mAccountName;
    private TextView mCardNumber;
    private TextView mBankName;
    private TextView mAccountCity;
    private TextView mBankBranch;
    private String businessId;
    private String cardUnitUkid;
    private BindCardBean.DataBean.ListBean data;
    private ElasticScrollView elasticScrollView;
    private AppCompatButton mManagerBankCard;
    private Bundle bundle;

    @Override
    protected int getContentId() {
        return R.layout.financial_center_fragment_card_infomation;
    }

    @Override
    public void setTitle() {
        mActivity.setTitle(getString(R.string.financial_center_card_infomation));
    }

    @Override
    protected void init(View view) {
        mAccountName = findView(view, R.id.tv_account_name);
        mCardNumber = findView(view, R.id.tv_card_number);
        mBankName = findView(view, R.id.tv_bank_name);
        mAccountCity = findView(view, R.id.tv_account_city);
        mBankBranch = findView(view, R.id.tv_bank_branch);
        elasticScrollView = (ElasticScrollView) view.findViewById(R.id.elastic_scroll);
        mManagerBankCard = findView(view, R.id.btn_manager_bank_card);
        bundle = getArguments();
        try {
            if (bundle != null) {
                data = (BindCardBean.DataBean.ListBean) bundle.getSerializable("data");
                mAccountName.setText(data.getAccountName());
                mCardNumber.setText(data.getCardNo());
                mBankName.setText(data.getBankName());
                mBankBranch.setText(data.getSubbankName());
                mAccountCity.setText(String.format("%s%s", data.getProvinceName(), data.getCityName()));
                businessId = bundle.getString("businessId");
                cardUnitUkid = bundle.getString("cardUnitUkid");
            }
        } catch (Exception e) {
        }

    }

    @Override
    protected void loadDatas() {
        Map<String, Object> map = new HashMap<>();
        map.put("businessUnitId", businessId);
        map.put("cardUnitUkid",cardUnitUkid);
        httpPost(FinancialConstant.GETUNITSBYCARDUKID, map, 0, false, "");


    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        if (what==0){
            if (TextUtils.equals("0", commonClass.getCode())) {
                try{
                    List<PermissionBean.DataBean> dataBeen = JSON.parseArray(commonClass.getData().toString(), PermissionBean.DataBean.class);
                    String clientShow = dataBeen.get(0).getClientShow();
                    if ("1".equals(clientShow)){
                        elasticScrollView.setOnPullListener(new ElasticScrollView.OnPullListener() {
                            @Override
                            public void onDownPull() {

                            }

                            @Override
                            public void onUpPull() {
                                mManagerBankCard.setVisibility(mManagerBankCard.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
                            }
                        });
                        mManagerBankCard.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                UnbindCardInfoFragment unbindCardInfoFragment = new UnbindCardInfoFragment();
                                assert bundle != null;
                                bundle.putString("isCardInfomation", "true");
                                unbindCardInfoFragment.setArguments(bundle);
                                pushFragment(unbindCardInfoFragment);
                            }
                        });

                    } else {
                        mManagerBankCard.setVisibility(View.GONE);
                    }
                }  catch (Exception e){
                    mManagerBankCard.setVisibility(View.GONE);
                }

            }   else {
                mManagerBankCard.setVisibility(View.GONE);
            }

        }
    }

    @Override
    public void onReLoad(int code) {

    }
}
